<?php
/**
 *   OPENSHOP 文件服务器图片处理类
 * 
 *   @link        http://baison.com.cn
 *   @copyright   Baison, Inc.
 *   @package     OpenShop
 *   @version     $Id: img.php,v 1.0 2009/08/01 09:35:58 modified $
 *   @author      FillBag <fillbag@hotmail.com>
 */

class image{
    var $width     = 0;
    var $height    = 0;	
    var $filesync_host = '';
    
    var $themes_folder = array(
        0 => array('images'),
        1 => array('data'),
        2 => array('data/brandlogo'),
        3 => array('data/color_img'),
		4 => array('data/afficheimg')
    );
    
	function image($haystacks=NULL)
    {
//    	if(empty($GLOBALS['filesync_host'])){
//    		die('Wrong host name/address');
//    	}
//    	$this->filesync_host = $GLOBALS['filesync_host'];
        
        $this->filesync_host = SOURCE_PATH;
    	
        if (empty($haystacks))
        {
            $this->haystacks = $_SERVER['REQUEST_URI'];
        }
        $arr_haystacks = explode(('.php'.(strpos($this->haystacks, '.php?') ? '?':'')), $this->haystacks);
        $haystacks = $arr_haystacks[1];
        $realpath = strpos($haystacks, '?') ? substr($haystacks, 0, strpos($haystacks, '?')) : $haystacks;
        $arr_size = (explode(',' ,substr($haystacks, strpos($haystacks, '?')+1)));
        if (!empty($realpath))
        {
            $this->realpath = $realpath;
        }

        $this->width    =(isset($arr_size[0]) && $arr_size[0] > 0) ? $arr_size[0] : 0;
        $this->height   = (isset($arr_size[1]) && $arr_size[1] > 0) ? $arr_size[1] : 0;
        
        //print($this->width.'--------'.$this->height);
        /* 检查是否支持 GD */
        if (PHP_VERSION >= '4.3')
        {

            return (function_exists('imagecreatetruecolor') || function_exists('imagecreate'));
        }
        else
        {

            return (((imagetypes() & IMG_GIF) > 0) || ((imagetypes() & IMG_JPG)) > 0 );
        }
    }
    
    function __construct($haystacks=NULL)
    {
        $this->image($haystacks);
    }   
    
	function generate_image()
    {
//        if (function_exists('imagecreatefromjpeg') && ((imagetypes() & IMG_JPG) > 0))
//        {
//
//        }
//        else
//        {
//
//        }

        $theme = $this->filesync_host . $this->realpath; 

        $arr_img_size = @getimagesize($theme);
        //不能访问 filename 指定的图像或者其不是有效的图像
        if(empty($this->realpath) || $arr_img_size===FALSE){
        	$theme = $this->filesync_host . '/' . $GLOBALS['_CFG']['no_picture']; //设置默认图片
        	$arr_img_size = @getimagesize($theme);
        }
        
		if(function_exists('imagecreatefromjpeg') && ((imagetypes() & IMG_JPG) > 0) && 
		                  (($arr_img_size[2] & IMG_JPG) > 0)){
		    $source = imagecreatefromjpeg($theme);
		}elseif(function_exists('imagecreatefromgif') && ((imagetypes() & IMG_GIF) > 0) 
		                    && (($arr_img_size[2] & IMG_GIF) > 0)){
			$source = imagecreatefromgif($theme);
		}elseif(function_exists('imagecreatefrompng') && ((imagetypes() & IMG_PNG) > 0) 
		                    && (($arr_img_size[2] & IMG_PNG) > 0)){
			$source = imagecreatefrompng($theme);
		}elseif(function_exists('imagecreatefromwbmp') && ((imagetypes() & IMG_WBMP) > 0) 
		                    && (($arr_img_size[2] & IMG_WBMP) > 0)){
			$source = imagecreatefromwbmp($theme);
		}else{
			die("No image support in this PHP server");
		}
		
		$initial_width  = imagesx($source);
		$initial_height = imagesy($source);
	
//		$b=$initial_width/$initial_height;
	
		$this->width = ($this->width > 0 ? $this->width : $initial_width);
		$this->height = ($this->height > 0 ? $this->height : $initial_height);
		
	    if(($this->width > $initial_width) || ($this->width <= 0)){
			$this->width = $initial_width;
		}
	    if(($this->height > $initial_height) || ($this->height <= 0)){
			$this->height = $initial_height;
		}
		
//		$basic_value = ($this->width < $this->height) ? $this->width : $this->height;
//		
//		if($basic_value == $this->width){
//			$this->height = $basic_value / $b;
//		}else{
//			$this->width = $b * $basic_value; 
//		}

		if(($this->width > $initial_width) && ($this->height > $initial_height)){
			$this->width = $initial_width;
			$this->height = $initial_height;
		}elseif($initial_width > $this->width || $initial_height > $this->height){
			$bit = $this->height/$this->width;
			$imgbit = $initial_height/$initial_width;
			if($imgbit>=$bit){
				$this->width = $initial_width*($this->height/$initial_height);
			}else{
				$this->height = $initial_height*($this->width/$initial_width);
			}
		}
		$this->width = round($this->width);
		$this->height  = round($this->height);

		$im   = ((function_exists('imagecreatetruecolor')) && PHP_VERSION >= '4.3') ?
		              imagecreatetruecolor($this->width, $this->height) : imagecreate($this->width, $this->height);
		
		/* 将背景图象复制原始图象并调整大小 */
		if (function_exists('imagecopyresampled') && PHP_VERSION >= '4.3') // GD 2.x
		{
		    imagecopyresampled($im, $source, 0, 0, 0, 0, $this->width, $this->height, $initial_width, $initial_height);
		}
		else // GD 1.x
		{
		    imagecopyresized($im, $source, 0, 0, 0, 0, $this->width, $this->height, $initial_width, $initial_height);
		}
		imagedestroy($source);
		
		//header('Expires: Thu, 01 Jan 1970 00:00:00 GMT');
		
		// HTTP/1.1
		//header('Cache-Control: private, no-store, no-cache, must-revalidate');
		header('Cache-Control: public, must-revalidate');
		//header('Cache-Control: post-check=0, pre-check=0, max-age=0', false);
		header('Cache-Control: post-check=3600, pre-check=3600, max-age=3600', false);
		
		// HTTP/1.0
		//header('Pragma: no-cache');
		header('Pragma: public');
		
		if (function_exists('imagecreatefromjpeg')  && ((imagetypes() & IMG_JPG) > 0))
		{
		    header('Content-type: image/jpeg');
		    imageinterlace($im, 1);
		    imagejpeg($im, false, 95);
		}
		elseif(function_exists('imagecreatefromgif') && ((imagetypes() & IMG_GIF) > 0))
		{
			header("Content-type: image/gif");
			imagegif($im);            	
		}
		elseif(function_exists('imagecreatefrompng') && ((imagetypes() & IMG_PNG) > 0))
		{
			header("Content-type: image/png");
			imagepng($im);           	
		}
		elseif(function_exists('imagecreatefromwbmp') && ((imagetypes() & IMG_WBMP) > 0))
		{
		    header("Content-type: image/vnd.wap.wbmp");
		    imagewbmp($im);            	
		}
		
		imagedestroy($im);
		
		return true;
	}
}

define('IN_OS', true);
require(dirname(__FILE__) . '/includes/init.php');
$img = new image();
$img->generate_image();
?>